//
//  CategoryViewController.m
//  Atlantis
//
//  Created by 何恩营 on 14-12-17.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

// 分类页面
#import "CategoryViewController.h"
#import "NetHandler.h"
#import "UIColor+AddColor.h"
#import "AlbumListViewController.h"
#import "Classify.h"
#import "UIImageView+WebCache.h"
#import "CategoryCell.h"


#define REUSE @"reuse"

@interface CategoryViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate>
// 数组
@property(nonatomic, retain) NSArray *array;
// 类别数组
@property(nonatomic, retain) NSMutableArray *categoryArr;
// 集合视图
@property(nonatomic, retain) UICollectionView *collectionView;

@end

@implementation CategoryViewController

// 内存管理
- (void)dealloc
{
    [_category release];
    // 数组
    [_array release];
    // 类别视图
    [_categoryArr release];
    // 集合视图
    [_collectionView release];
    [super dealloc];
}


// 初始化
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        
    }
    return self;
}

// 视图将要出现
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // 调用处理数据
    [self handleData];
    //[self setFullScreen:YES];
}

//- (void)viewWillDisappear:(BOOL)animated
//{
//    [self setFullScreen:NO];
//}
//
//- (void)setFullScreen:(BOOL)fullScreen
//{
//    // 状态条
//    [UIApplication sharedApplication].statusBarHidden = fullScreen;
//    // 导航条
//    [self.navigationController setNavigationBarHidden:fullScreen];
//    // tabBar的隐藏通过在初始化方法中设置hidesBottomBarWhenPushed属性来实现
//}


// 处理数据
- (void)handleData
{
    
    // 创建请求
    NSString *str = [NSString stringWithFormat: @"http://mobile.ximalaya.com/m/category_tag_list?category=%@&device=iPhone&type=album", self.category.name];
    
    // 建立连接
    
    [NetHandler getDataWithURL:str completion:^(NSData *data) {
        
        // JSON解析
        NSError *error = nil;
        
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        // 创建数组
        self.array = [NSArray array];
        // 数组的赋值
        self.array = [result objectForKey:@"list"];
        // 创建类别数组
        self.categoryArr = [NSMutableArray array];
        // 循环获取数据
        for (NSMutableDictionary *tempDic in self.array) {
            // 创建对象
            Classify *classify = [[Classify alloc] initWithDictionary:tempDic];
            // 将对象存放到类别数组中
            [self.categoryArr addObject:classify];
            // 内存管理
            [classify release];
        }
        
        // 刷新视图
        [self.collectionView reloadData];
        
    }];

}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置视图的背景颜色
    self.view.backgroundColor = [UIColor redColor];
    // 设置导航栏的标题
    self.navigationItem.title = self.category.title;
    
    // UICollectionViewFlowLayout 是一个layout类的基类, 不能直接使用, 它只是规定了一些必须要重写的方法和属性
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc ]init];
    // 设置flowLayout的大小
    flowLayout.itemSize = CGSizeMake(self.view.bounds.size.width / 3, self.view.bounds.size.width / 3 + 20);
    
    //滚动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    //最小item的间距
    flowLayout.minimumInteritemSpacing = 0;
    //最小行间距
    flowLayout.minimumLineSpacing = 0;
    
    // 创建集合视图, 并设置大小
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) collectionViewLayout:flowLayout];
    [flowLayout release];
    // 设置集合视图的背景颜色
    self.collectionView.backgroundColor = [UIColor colorFromHexCode:@"#e6e6e6"];
    // 指定代理人
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    // 将集合视图加载到视图上
    [self.view addSubview:self.collectionView];
    // 内存管理
    [_collectionView release];
    
    
    // 设置集合视图的中心大小
    self.collectionView.contentSize = CGSizeMake(0, self.view.bounds.size.height);
    
    //collectionView必须要注册一个cell类
    [self.collectionView registerClass:[CategoryCell class] forCellWithReuseIdentifier:REUSE];
    
    //[self.collectionView reloadData];
    // Do any additional setup after loading the view.

    // 导航栏左侧返回按钮
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"return.png"] style:UIBarButtonItemStyleDone target:self action:@selector(collectionAction:)] autorelease];
//    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(collectionAction:)] autorelease];

}

// 导航栏左侧返回按钮触发事件
- (void)collectionAction:(UINavigationBar *)bar
{
    //NSLog(@"返回前一页");
    // 推出前一页
    [self.navigationController popViewControllerAnimated:YES];
}

//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
    // 创建视图
    AlbumListViewController *albumListVC = [[AlbumListViewController alloc]init];
    // 赋值
    albumListVC.category_name = self.category.name;
    // 赋值
    albumListVC.tag_name = [[self.categoryArr objectAtIndex:indexPath.item] tname];
    // 推出界面
    [self.navigationController pushViewController:albumListVC animated:YES];
    // 释放内存
    [albumListVC release];
    
}

// cell的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.categoryArr count];
}

// cell的赋值
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CategoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:REUSE forIndexPath:indexPath];
    
    //系统提供的cell没有任何自带的控件,完全需要我们自定义
    //cell.contentView.backgroundColor = [UIColor blueColor];
    
    // 创建对象
    Classify *classify = [self.categoryArr objectAtIndex:indexPath.row];
    // 自定义cell的对象进行赋值
    cell.classify = classify;
 
    return cell;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
